package com.sunny.springboot2.controller;

import com.sunny.springboot2.core.annotation.DateTime;
import com.sunny.springboot2.entity.Groups;
import com.sunny.springboot2.entity.ValidGroups;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;

/**
 * @author sunny
 * @date 2018/11/30
 */
@RestController
@RequestMapping("/noPer/validator")
@Validated
public class ValidateController {

    @GetMapping("/valid")
    public String valid(@NotNull(message = "date不能为空") @DateTime(message = "日期格式错误，正确格式为：{format}")  String date) {
        return "success - " + date;
    }

    /**
     * @Validated(value = Groups.Default.class) ValidGroups中的id的groups为Update 这里不属于一个组，所以不会对id进行校验
     *
     * @param groups
     * @author sunny
     * @date 2018/11/30
     * @return java.lang.String
     */
    @GetMapping("/insert")
    public String insert(@Validated(value = Groups.Default.class) ValidGroups groups) {
        return "insert";
    }

    /**
     * @Validated(value = {Groups.Default.class, Groups.Update.class}) 表示ValidGroups中groups属于以上两种分组的参数都要校验
     *
     * @param groups
     * @author sunny
     * @date 2018/11/30
     * @return java.lang.String
     */
    @GetMapping("/update")
    public String update(@Validated(value = {Groups.Default.class, Groups.Update.class}) ValidGroups groups) {
        return "update";
    }
}
